package 数据结构和算法.数据结构.普通二叉树;

public class Test {
    public static void main(String[] args) {
        // 构造一棵树
        TreeNode treeNode = new TreeNode(10);
        TreeNode treeNode1 = new TreeNode(9);
        TreeNode treeNode2 = new TreeNode(20);
        TreeNode treeNode3 = new TreeNode(15);
        TreeNode treeNode4 = new TreeNode(35);

        treeNode.setLeftTreeNode(treeNode1);
        treeNode.setRightTreeNode(treeNode2);
        treeNode2.setLeftTreeNode(treeNode3);
        treeNode2.setRightTreeNode(treeNode4);

        // 调用前序遍历
        //preTraverseBTree(treeNode);

        // 中序遍历
        //inTraverseBTree(treeNode);

        // 后序遍历
        afterTraverseBTree(treeNode);
    }

    // 前序遍历
    public static void preTraverseBTree(TreeNode rootNode) {
        if (rootNode != null) {
            System.out.println(rootNode.getValue());

            preTraverseBTree(rootNode.getLeftTreeNode());

            preTraverseBTree(rootNode.getRightTreeNode());
        }
    }

    // 中序遍历
    private static void inTraverseBTree(TreeNode rootNode) {
        if (rootNode != null) {
            inTraverseBTree(rootNode.getLeftTreeNode());

            System.out.println(rootNode.getValue());

            inTraverseBTree(rootNode.getRightTreeNode());
        }
    }

    // 后序遍历
    private static void afterTraverseBTree(TreeNode rootNode) {
        if (rootNode != null) {
            afterTraverseBTree(rootNode.getLeftTreeNode());

            afterTraverseBTree(rootNode.getRightTreeNode());

            System.out.println(rootNode.getValue());
        }

    }
}
